package com.apes.fn.scm.contract;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * Creator: zouyc
 * CreateDate: 2019-12-10 15:14
 * Description:
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FnContract.CONST_TABLE_NAME)
public class FnContract extends Model {
    public static final String CONST_TABLE_NAME = "fn_contract";

    @Id
    @Column(name = "id", length = 30)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "赋能合同编码", padding = 12)
    @Comment("合同编码")
    private String id;

    @Name
    @Column(name = "name", length = 30)
    @Comment("合同名称")
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id") // todo , nullable = false
    @Comment("公司")
    private PartyRole company; // 公司

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    @Comment("部门")
    private Dept saleDept; //部门

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "partner_id", referencedColumnName = "id", nullable = false)
    @Comment("合作伙伴（赋能公司）")
    private PartyRole partner; // 合作伙伴_赋能公司

    @Column(name = "store_deposit")
    @Comment("门店保证金(元/店)")
    private double storeDeposit = 0;

    @Column(name = "base_deposit")
    @Comment("基地保证金(元/店)")
    private double baseDeposit = 0;

    @Column(name = "stock_deposit")
    @Comment("常备库存定金(万元/店)")
    private double stockDeposit = 0;

    @Temporal(TemporalType.DATE)
    @Column(name = "valid_from")
    @Comment("有效期起")
    private Date validFrom;

    @Temporal(TemporalType.DATE)
    @Column(name = "valid_period")
    @Comment("有效期止")
    private Date validPeriod;

    @Column(name = "state")
    @Comment("状态(create:创建,delete:删除,approve:审核,repeal:终止,extension:延期,activation:激活)")
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "delete_date")
    @Comment("删除日期")
    private Date deleteDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "repeal_uid", referencedColumnName = "id")
    @Comment("终止人")
    private PartyRole repealUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "repeal_date")
    @Comment("终止日期")
    private Date repealDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "extension_uid", referencedColumnName = "id")
    @Comment("延期人")
    private PartyRole extensionUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "extension_date")
    @Comment("延期日期")
    private Date extensionDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "activation_uid", referencedColumnName = "id")
    @Comment("激活人")
    private PartyRole activationUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "activation_date")
    @Comment("激活日期")
    private Date activationDate;

    @Transient
    private PartyRole _creator;
    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }
}
